package expression

var (
	defaultExp = CreateExecer("")
)

func Exec(ctx ExpContext, exp string) (*OutData, error) {
	return defaultExp.Exec(ctx, exp)
}

// 执行方法 返回bool 值
func ExecParamBool(exp string, vars map[string]interface{}) (bool, error) {
	outdata, _ := ExecParam(exp, vars)
	return Bool(outdata)
}

// 执行表达式 返回字符串切片
func ExecParamStringSlice(exp string, vars map[string]interface{}) ([]string, error) {
	return SliceStr(ExecParam(exp, vars))
}

func ExecParam(exp string, vars map[string]interface{}) (*OutData, error) {
	ectx := CreateExpContext()
	for key, v := range vars {
		ectx.AddVar(key, v)
	}
	return defaultExp.Exec(ectx, exp)
}

func Bool(d *OutData, err ...error) (bool, error) {
	if len(err) > 1 && err[0] != nil {
		return false, err[0]
	}
	return d.Bool()
}

func SliceStr(d *OutData, err ...error) ([]string, error) {
	if len(err) > 1 && err[0] != nil {
		return nil, err[0]
	}
	return d.SliceStr()
}
